angular.module('myApp', ['ui.bootstrap','ui.router'])
.config(['$locationProvider', function($locationProvider) {  
  $locationProvider.html5Mode(true);  
}])
    .controller('SearchCtrl',  [ '$http','$scope','$state','$location', function ($http,$scope,$state,$location) {

        $scope.prodcuts=function () {
            
        };


        $scope.params=new Object();
        $scope.params.trade="";
        $scope.params.product="";
        $scope.params.institution="";
        $scope.params.frequency="";
        $scope.params.author="";
        $scope.params.title="";




        $scope.pagination=new Object();

        $scope.metadata=new Object();

        $scope.loadMetaData=function () {

            $http({
                url:"/api/search/metadata/",
                method:'GET'
            }).success(function(data,header,config,status){
                $scope.metadata=data.data;


            }).error(function(data,header,config,status){
            //处理响应失败
                console.log(data);
            });

        }
        $scope.getProducts=function (trade) {

            var url="/api/products/?trade="+trade;

            $http({
                url:url,
                method:'GET'
            }).success(function(data,header,config,status){
                $scope.metadata.products=data.data;


            }).error(function(data,header,config,status){
            //处理响应失败
                console.log(data);
            });
        };
        $scope.setPage=function(currentPage,totalPage){
            $scope.pagination.currentPage=currentPage;
            $scope.pagination.totalPage=totalPage
            if (currentPage<totalPage){
                $scope.pagination.hasNext=true;
            }else{
                $scope.pagination.hasNext=false;
            }
            if (currentPage>1){
                $scope.pagination.hasPrevious=true;
            }

            startIndex=1;
            endStart=totalPage;
            if (currentPage>5 && totalPage>(currentPage+5)){
                startIndex=currentPage-5
                endStart=currentPage+5
            }else if(currentPage>5 && totalPage<=(currentPage+5)){
                startIndex=currentPage-5
            }else if (currentPage<=5 && totalPage>(currentPage+5)){
                startIndex=1;
                endStart=currentPage+5
            }
            

        }
        
        $scope.toReportDetail=function (id) {
            location.href="/report/"+id+"/detail/";
        };
        $scope.loadData=function () {
             var url="/api/search/reports/?trade="+$scope.params.trade+"&product="+$scope.params.product+"&frequency="+$scope.params.frequency+"&institution="+$scope.params.institution+"&author="+$scope.params.author+"&title="+$scope.params.title;

            $http({
                url:url,
                method:'GET'
            }).success(function(data,header,config,status){
                $scope.data=data.data;
                

            }).error(function(data,header,config,status){
            //处理响应失败
                console.log(data);
            });
        };
        $scope.select_trade=function (code) {

            $scope.params.trade=code;
            console.log($scope.params);
            
        };
        $scope.select_frequency=function(code){
            $scope.params.frequency=code;
        };
        $scope.select_institution=function(code){
            $scope.params.institution=code;
        };
    $scope.$watch('params.trade',function(newValue,oldValue, scope) {
        $scope.loadData();
    });
    $scope.$watch('params.product',function(newValue,oldValue, scope) {
        $scope.loadData();
    });
    $scope.$watch('params.frequency',function(newValue,oldValue, scope) {
        $scope.loadData();
    });
        $scope.$watch('params.institution',function(newValue,oldValue, scope) {
        $scope.loadData();
    });
        $scope.$watch('params.title',function(newValue,oldValue, scope) {
        $scope.loadData();
    });
          $scope.$watch('params.author',function(newValue,oldValue, scope) {
        $scope.loadData();
    });
        $scope.loadMetaData();
         $scope.loadData();
    if ($location.search().trade) {  
            $scope.params.trade = $location.search().trade;  
    }
        if ($location.search().frequency) {
            $scope.params.frequency = $location.search().frequency;
    }
        if ($location.search().institution) {
            $scope.params.institution = $location.search().institution;
    }
                if ($location.search().author) {
            $scope.params.author = $location.search().author;
    }
                if ($location.search().title) {
            $scope.params.title = $location.search().title;
    }
}]);

